<template>
  <div style="width: 100%;height: 100%;">
    <div class="header">
      <div class="logo">
        <img src="@/assets/img/company-logo.png" alt />
        <span>{{systemTitle}}</span>
      </div>
      <div style="flex:1;">
        <div class="user-info right">
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="el-dropdown-link">
              <i class="el-icon-user-solid" style="color: #DDDDDD;"></i>
              {{username}}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="loginout">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <el-menu
          mode="horizontal"
          :default-active="pageRoute"
          class="el-menu-top right"
          text-color="#333333"
          active-text-color="#1492FF"
        >
          <el-menu-item
            v-for="(item,index) in applications"
            :key="index"
            :index="item.id"
            @click="changeNav(item)"
          >{{item.name}}</el-menu-item>
        </el-menu>
      </div>
    </div>
    <div class="nav-left">
      <div class="nav-title">
        <span>政务百科系统</span>
        <i class="el-icon-s-fold"></i>
      </div>
      <el-menu
        :default-active="currentPath"
        class="el-menu-vertical"
        text-color="#333"
        active-text-color="#1492ff"
        router
      >
        <!-- 一级菜单 -->
        <template v-for="(item, index) in menuList">
          <el-submenu v-if="item.childList.length>0" :index="item.url" :key="index">
            <template slot="title">
              <p style="padding-left:20px;">
                <i class="icon iconfont" :class="item.icon"></i>
                {{item.name}}
              </p>
            </template>
            <!-- 二级菜单 -->
            <template v-for="(subItem, subIndex) in item.childList">
              <el-submenu v-if="subItem.childList.length>0" :key="subIndex" :index="subItem.url">
                {{subItem.name}}
                <!-- 三级菜单-->
                <el-menu-item
                  v-for="(minItem,minIndex) in subItem.childList"
                  :key="minIndex"
                  :index="minItem.url"
                >{{minItem.name}}</el-menu-item>
              </el-submenu>
              <!-- 三级菜单 -->
              <el-menu-item v-else :key="subIndex" :index="subItem.url">
                <template slot="title">
                  <i class="icon iconfont" :class="item.icon"></i>
                  {{subItem.name}}
                </template>
              </el-menu-item>
            </template>
          </el-submenu>
          <el-menu-item v-else :key="index" :index="item.url">
            <template slot="title">
              <i class="icon iconfont" :class="item.icon"></i>
              {{item.name}}
            </template>
          </el-menu-item>
        </template>
      </el-menu>
    </div>
  </div>
</template>
<script>
  import * as paramsApi from '@/api/paramsApi.js';
  export default {
    data() {
      return {
        active: '',
        menuList: [],
        pageRoute: '',
        pageTitle: '',
        username:'',
        menuObject:{},
        applications: [], // 应用列表
        isCollapse: true,
        currentPath:'',
        showLabel: true,
        systemTitle:''
      }
    },
    methods:{
      handleCommand(command) {
        if (command == 'loginout') {
          localStorage.clear();
          // localStorage.removeItem('loginToken');
          // localStorage.removeItem('authorization');
          // localStorage.removeItem('isLogin');
          // localStorage.removeItem('menuList');
          this.$router.push('/login');
        }
      },
      // 获取菜单列表
      // 获取登录是存入缓存的菜单数据
      getPowerMenuList(){
        paramsApi.getPowerMenu().then(res => {
          if(res.data.statusCode == '200'){
            const menus = res.data.data;
            // 获取默认菜单列表
            this.menuObject = res.data.data;
            let appList = []; // 获取应用列表
            for(let i in menus){
              if(menus[i].length>0 && i === 'gov-affair-clopedia'){
                let menu = menus[i];
                appList.push({id:menu[0].affiliationApp, name:menu[0].affiliationAppName});
              }
            }
            this.applications = appList;
            const defaultId = appList[0].id;
            this.systemTitle = appList[0].name;
            this.menuList = menus[defaultId];
            this.pageRoute = defaultId;
          } else {
            if(res.data.msg.length > 20){
              this.$message.error('服务器接口异常，请联系管理员');
            } else {
              this.$message.error(res.data.msg);
            }
          }
        })
      },
      // 切换顶部导航
      changeNav(item){
        const list = this.menuObject;
        const currentId = item.id;
        this.pageRoute = item.id;
        this.menuList = list[currentId];
        this.currentPath=location.href.split('#')[1];
        if(this.currentPath === this.menuList[0].url){
          return;
        } else {
          this.$router.push(this.menuList[0].url);
        }
        console.log('currentPath:',this.currentPath);
      }
    },
    created(){
      const loginToken = localStorage.getItem('loginToken');
      this.username = JSON.parse(loginToken).userName;
      this.currentPath=location.href.split('#')[1];
      this.getPowerMenuList();
    },
    // handleOpen(){},
    // handleClose(){}
  }
</script>

<style scoped>
.nav-title {
  height: 56px;
  line-height: 56px;
  text-align: center;
  width: 100%;
  min-width: 150px;
  box-sizing: border-box;
  background: #1492ff;
  color: #fff;
  font-size: 16px;
}
.nav-title i {
  font-size: 25px;
  color: #fff;
}
a {
  display: inline-block;
  text-decoration: none;
  outline: none;
  color: inherit;
  margin-left: 5px;
}
.el-menu {
  overflow: hidden;
  border-right: none;
}
.el-menu-item {
  text-align: center;
}
.el-menu-top.el-menu--horizontal.el-menu li {
  height: 80px;
  font-size: 18px;
  line-height: 80px;
}

.el-menu-top.el-menu--horizontal.el-menu .el-menu-item:hover {
  background: #fff;
  color: rgb(20, 146, 255) !important;
  border-bottom-color: rgb(20, 146, 255) !important;
  border-bottom-width: 4px;
}

.el-menu-top.el-menu--horizontal.el-menu .el-menu-item.is-active {
  background: #fff;
  border-bottom-width: 4px;
}

.el-menu-top {
  display: inline-block;
  width: auto;
  background: none;
  border-bottom: none;
}

.sidebar {
  display: block;
  position: absolute;
  width: 12.19512%;
  left: 0;
  top: 80px;
  bottom: 0;
  background: #2e363f;
}

ul.sidebar {
  border: none;
  height: 100%;
  width: 12.19512% !important;
  padding: 0;
  margin: 0;
}

ul.sidebar > li.el-menu-item {
  text-align: center;
}

.sidebar.el-menu .el-menu-item.is-active,
.sidebar.el-menu li:hover {
  background: rgba(20, 146, 255, 0.04) !important;
}

.sidebar.el-menu .el-menu-item.is-active:after,
.sidebar.el-menu li:hover:after {
  content: "";
  display: block;
  width: 4px;
  height: 56px;
  background: #1492ff;
  position: absolute;
  left: 0;
  top: 0;
}

.header {
  position: relative;
  display: flex;
  box-sizing: border-box;
  width: 100%;
  height: 80px;
  font-size: 22px;
  line-height: 80px;
  color: #fff;
  background: #ffffff;
  text-align: center;
}

.header .logo {
  float: left;
  padding-left: 20px;
  height: 80px;
}

.header .logo img {
  height: 48px;
  float: left;
  padding-top: 10px;
  margin-top: 9px;
}

.header .logo span {
  font-size: 30px;
  font-family: PingFangSC;
  font-weight: 600;
  color: rgba(51, 51, 51, 1);
  line-height: 42px;
  padding-left: 10px;
}

.user-info {
  float: right;
  padding-right: 50px;
  font-size: 16px;
  color: #fff;
}

.user-info .el-dropdown-link {
  position: relative;
  display: inline-block;
  padding-left: 50px;
  line-height: 80px;
  color: #333333;
  cursor: pointer;
}

.user-info .user-logo {
  position: absolute;
  left: 0;
  top: 15px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.nav-left {
  width: 13%;
  height: 100%;
}
.el-menu-vertical {
  min-height: 400px;
  height: auto;
  height: calc(100% - 30px);
}
.el-menu-vertical .el-menu-item {
  padding-left: 40px !important;
  text-align: left;
}
.el-submenu__title {
  padding-left: 40px !important;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  min-width: 150px;
}
.nav-left .el-menu {
  overflow-y: auto !important;
}
.el-menu::-webkit-scrollbar {
  display: none;
}
</style>
